package org.csu.pet_store_back.web;

import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpSession;
import jakarta.validation.Valid;
import org.csu.pet_store_back.common.CommonResponse;
import org.csu.pet_store_back.domain.Order;
import org.csu.pet_store_back.service.OrderService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Validated
public class OrderController {
    @Resource
    private OrderService orderService;

    /**
     * 按照用户名获取用户订单
     * */
    @GetMapping("/orders")
    public CommonResponse<List<Order>> getOrders(
            @RequestParam("userid") String username,
            HttpSession session){
        CommonResponse<List<Order>> result = orderService.getAllOrders(username);
        if (result.isSuccess()){
            session.setAttribute("orderList",result.getData());
        }
        return result;
    }

    /**
     * 创建新订单并插入数据库
     * */
    @PostMapping("/orders")
    public CommonResponse<Order> newOrders(@Valid @RequestBody Order order){
        return orderService.newOrder(order);
    }

    /**
     * 更新订单状态之修改发货地址
     * */
    @PutMapping("/orders/address")
    public CommonResponse<Order> updateOrderAddress(
            @RequestParam("billAddress") String address,
            @RequestParam("orderId") String orderId
    ){
        // TODO:更新订单的发货地址
        return orderService.updateOrderAddress(orderId,address);
    }

    /**
     * 订单发货,修改订单状态，新增发货时间
     * */
    @PutMapping("/orders")
    public CommonResponse<Order> updateOrderStatus(
            @RequestParam("paymentStatus") String status,
            @RequestParam("orderId") String orderId,
            @RequestParam("sendDate") String date
    ){
        // TODO:发送订单
        return orderService.updateOrderStatus(status, orderId, date);
    }
}
